//
//  VKVoucherController.m
//  V-Learn
//
//  Created by liubo on 2019/1/10.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKVoucherController.h"
#import "VKVoucherChildController.h"
@interface VKVoucherController ()<ZJScrollPageViewDelegate>
@property(strong, nonatomic)NSMutableArray<NSString *> *titles;
@property(strong, nonatomic)NSMutableArray<NSString *> *ids;

@property(strong, nonatomic)NSArray<UIViewController *> *childVcs;
@property (nonatomic, strong) ZJScrollPageView *scrollPageView;
@property (nonatomic, strong) UIButton *deleteButton;
@property (nonatomic, strong) UIButton *managerBtn;
@property (nonatomic, assign) NSInteger index;

@end

@implementation VKVoucherController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"优惠券";
//    self.navigationController.title = @"代金券";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.edgesForExtendedLayout = @[];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupScrollView];
    [self setupRightItem];
    
    [self addDeleteBtn];
}
- (void)addDeleteBtn{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    _deleteButton = button;
    button.hidden = YES;
    [button setTitle:@"删除" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor cz_colorWithRed:238 green:81 blue:39];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@50);
        make.width.mas_equalTo([UIScreen cz_screenWidth]);
        make.bottom.mas_equalTo(self.view);
    }];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonClick{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"ManagerDelete" object:nil];
}
// 右边Item
- (void)setupRightItem{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    _managerBtn = button;
    button.frame = CGRectMake(0, 0, 40, 30);
    [button setTitle:@"管理" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor cz_colorWithRed:40 green:40 blue:40] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button] ;
//    [button mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.equalTo(@30);
//        make.width.equalTo(@40);
//    }];
    [button addTarget:self action:@selector(buttonItemClick) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonItemClick{
    if ([_managerBtn.titleLabel.text isEqualToString:@"管理"]) {
        [_managerBtn setTitle:@"取消" forState:UIControlStateNormal];
        _deleteButton.hidden = NO;
    }else if ([_managerBtn.titleLabel.text isEqualToString:@"取消"]){
        [_managerBtn setTitle:@"管理" forState:UIControlStateNormal];
        _deleteButton.hidden = YES;
    }
//    [[NSNotificationCenter defaultCenter]postNotificationName:[NSString stringWithFormat:@"ManagerY%ld",self.index] object:@{@"ManagerHidden":@(_deleteButton.hidden)}];
    [[NSNotificationCenter defaultCenter]postNotificationName:[NSString stringWithFormat:@"ManagerY"] object:@{@"ManagerHidden":@(_deleteButton.hidden)}];

}
- (void)setupScrollView{
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    //显示滚动条
    style.showLine = YES;
    style.scrollLineColor = [UIColor cz_colorWithRed:253 green:79 blue:83];
    style.scrollLineHeight = 1.5;
    style.scaleTitle = NO;
    style.scrollContentView = NO;
    //    style.scrollTitle = NO;
    style.autoAdjustTitlesWidth = YES;
    style.titleFont = [UIFont systemFontOfSize:16];
    style.normalTitleColor = [UIColor cz_colorWithRed:103 green:103 blue:103];
    style.selectedTitleColor = [UIColor cz_colorWithRed:40 green:40 blue:40];
    style.titleMargin = 30;
    // 颜色渐变
    style.gradualChangeTitleColor = YES;
    _titles = @[@"未使用",@"已使用",@"已过期"].mutableCopy;
    // 初始化
    self.scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    [self.view addSubview:self.scrollPageView];
}

//  以下
- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    
    if (!childVc) {
        childVc = [[VKVoucherChildController alloc] initWithId:index + 1];
    }
    
//    NSLog(@"点击:%ld--------------------------",(long)index);
    _index = index + 1;
    return childVc;
}


- (BOOL)shouldAutomaticallyForwardAppearanceMethods {
    return NO;
}

@end
